package server.maps;

import java.awt.Point;

import provider.DbgData;
import provider.DbgDataTool;
import server.maps.DbgGenericPortal;
import server.maps.DbgMapPortal;

public class PortalFactory {

    private int nextDoorPortal = 0x80;

    public DbgPortal makePortal(int type, DbgData portal) {
	DbgGenericPortal ret = null;
	if (type == DbgPortal.MAP_PORTAL) {
	    ret = new DbgMapPortal();
	} else {
	    ret = new DbgGenericPortal(type);
	}
	loadPortal(ret, portal);
	return ret;
    }

    private void loadPortal(DbgGenericPortal myPortal, DbgData portal) {
	myPortal.setName(DbgDataTool.getString(portal.getChildByPath("pn")));
	myPortal.setTarget(DbgDataTool.getString(portal.getChildByPath("tn")));
	myPortal.setTargetMapId(DbgDataTool.getInt(portal.getChildByPath("tm")));
	myPortal.setPosition(new Point(DbgDataTool.getInt(portal.getChildByPath("x")), DbgDataTool.getInt(portal.getChildByPath("y"))));
	String script = DbgDataTool.getString("script", portal, null);
	if (script != null && script.equals("")) {
	    script = null;
	}
	myPortal.setScriptName(script);

	if (myPortal.getType() == DbgPortal.DOOR_PORTAL) {
	    myPortal.setId(nextDoorPortal);
	    nextDoorPortal++;
	} else {
	    myPortal.setId(Integer.parseInt(portal.getName()));
	}
    }
}
